﻿using System.Threading.Tasks;
using System;
using Microsoft.EntityFrameworkCore;
using Oracle.ManagedDataAccess.Client;
using Microsoft.AspNetCore.Http;
using Simple.Services.System;
using Simple.Services.System.QHSEUser.Models;
using Simple.Services.System.QHSEViolationCategory.Models;

namespace Simple.Services;

public class QHSEViolationObserverService
{
    private readonly SimpleDbContext _context;
    public QHSEViolationObserverService(SimpleDbContext context)
    {
        _context = context;
    }
    public async Task<List<QHSEViolationObserverModel>> GetAsync()
    {
        var violationObservers = await _context.Set<QHSESysViolationObserver>().AsNoTracking().Where(u => u.Note == null).ToListAsync();
        return MapperHelper.Map<List<QHSEViolationObserverModel>>(violationObservers);
    }
    public async Task<List<QHSEViolationObserverModel>> GetObserverAsync()
    {
        var violationObservers = await _context.Set<QHSESysViolationObserver>().AsNoTracking().Where(u=>u.Note==null).GroupBy(u=>u.Item1).Select(u=>u.FirstOrDefault()).ToListAsync();
        return MapperHelper.Map<List<QHSEViolationObserverModel>>(violationObservers);
    }
    public async Task<List<QHSEViolationObserverModel>> GetObserverAsync(SimpleDbContext context)
    {
        var violationObservers = await context.Set<QHSESysViolationObserver>().AsNoTracking().Where(u => u.Note == null).GroupBy(u => u.Item1).Select(u => u.FirstOrDefault()).ToListAsync();
        return MapperHelper.Map<List<QHSEViolationObserverModel>>(violationObservers);
    }
    public async Task<List<QHSEViolationObserverModel>> GetObserver2Async(string item)
    {
        var violationObservers = await _context.Set<QHSESysViolationObserver>().AsNoTracking().Where(u => u.Note == null).Where(u => u.Item1 == item).ToListAsync();
        return MapperHelper.Map<List<QHSEViolationObserverModel>>(violationObservers);
    }
}
